﻿#include "EditorNotificationView.h"
#include "EditorNotificationViewModel.h"
#include "EditorNotificationItemView.h"
#include "EditorNotificationItemViewModel.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"
#include "Asset/EditorDefaultPackage.h"

#include "Widgets/EditorImageButton.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/Positioners/VerticalPositionerWidget.h"


void REditorNotificationView::BindViewModel(TSharedObjectPtr<REditorNotificationViewModel> InViewModel)
{
    ViewModel = InViewModel;

    UpdateItemViews();

    ViewModel->OnItemsChanged.Bind([this]()
    {
        UpdateItemViews();
        UpdateItemWidgets();
    });
}

void REditorNotificationView::FillWidgets(TSharedObjectPtr<RWidget> InParentWidget)
{
    //传入的父控件是覆盖整个窗口的，所以这里需要设置位置和大小
    auto NotificationWidget =  NewWidget<RWidget>(InParentWidget);
    NotificationWidget->Width = 400;
    NotificationWidget->Anchor.RightReference= InParentWidget;
    NotificationWidget->Anchor.RightOffset = 10;
    NotificationWidget->Anchor.BottomReference = InParentWidget;
    NotificationWidget->Anchor.BottomOffset = 20;
    const float TitleHeight = 30.f;
    NotificationWidget->Height = [this,TitleHeight]()->float
    {
        return ViewModel->GetItemsHeight()
            + (ViewModel->IsTitleVisible() ? TitleHeight : 0.f);
        ;
    };

    auto BackgroundWidget = NewWidget<RRectangleWidget>(NotificationWidget);
    BackgroundWidget->Anchor.Fill(NotificationWidget);
    BackgroundWidget->FillColor =REditor::Get()->CurrentTheme->ContextMenuBackgroundColor;
    BackgroundWidget->Radius= REditor::Get()->CurrentTheme->ContextMenuRadius;
    BackgroundWidget->BorderSize=REditor::Get()->CurrentTheme->ContextMenuBorderSize;
    BackgroundWidget->BorderColor=REditor::Get()->CurrentTheme->ContextMenuBorderColor;

    const float HorizontalPadding= 10.f;
    auto TitleWidget = NewWidget<RWidget>(NotificationWidget);
    TitleWidget->Height = TitleHeight;
    TitleWidget->Anchor.LeftReference = NotificationWidget;
    TitleWidget->Anchor.LeftOffset = HorizontalPadding;
    TitleWidget->Anchor.RightReference = NotificationWidget;
    TitleWidget->Anchor.RightOffset = HorizontalPadding;
    TitleWidget->Visible=[this]()->bool
    {
        return ViewModel->IsTitleVisible();
    };

    auto TitleTextWidget = NewWidget<RTextWidget>(TitleWidget);
    TitleTextWidget->Text =[this]()
    {
        if(ItemViews.Empty())
        {
            return U"无新通知";
        }
        return U"通知";
    };
     

    auto TitleButtons= NewWidget<RHorizontalPositionerWidget>(TitleWidget);
    const float ButtonSize = 20.f;
    TitleButtons->Height = ButtonSize;
    TitleButtons->Anchor.VerticalCenterReference= TitleWidget;
    TitleButtons->Anchor.RightReference = TitleWidget;
    {
        const float Spacing = 5.f;
        TitleButtons->Spacing= Spacing;
        TitleButtons->Width = ButtonSize * 3+ Spacing*2;

        auto ClearButton = WHE::CreateImageButton(
            REditorDefaultPackage::Get()->GetIcon(U"CodiconClearAll")
            ,CString(U"清空所有通知")
            ,[this]()
            {
                ViewModel->ClearItems();
            } 
            ,TitleButtons
            ,ButtonSize
            ,ButtonSize
            );

        auto ConfigButton = WHE::CreateImageButton(
            REditorDefaultPackage::Get()->GetIcon(U"CodiconBellSlash")
            ,CString(U"请勿打扰")
            ,[this]()
            {
                ViewModel->ToggleDonDisturb();
            }
            ,TitleButtons
            ,ButtonSize
            ,ButtonSize
            );

        auto HideButton = WHE::CreateImageButton(
            REditorDefaultPackage::Get()->GetIcon(U"CodiconChevronDown")
            ,CString(U"隐藏通知")
            ,[this]()
            {
                ViewModel->HideWholeNotification();
            }
            ,TitleButtons
            ,ButtonSize
            ,ButtonSize
            );
    }


    ItemsParentWidget = NewWidget<RVerticalPositionerWidget>(NotificationWidget);
    ItemsParentWidget->Anchor.HorizontalFill(NotificationWidget);
    ItemsParentWidget->Y=[this,TitleHeight]()->float
    {
        return ViewModel->IsTitleVisible() ? TitleHeight : 0.f;
    };
    ItemsParentWidget->Height = [this]()->float
    {
        return ViewModel->GetItemsHeight();
    };
    ItemsParentWidget->Spacing=0.f;
   
    //填充通知项
    UpdateItemWidgets();

}

void REditorNotificationView::UpdateItemViews()
{
    ItemViews.Clear();
    for (int64_t i = 0; i < ViewModel->GetItemsCount(); ++i)
    {
        auto ItemViewModel = ViewModel->GetItem(i);
        auto ViewClass=REditor::Get()->GetViewClass(ItemViewModel->GetClass());
        auto ItemView = NewObject<REditorNotificationItemView>(ViewClass,this);
        ItemView->BindViewModel(ItemViewModel);
        ItemViews.Add(ItemView);
    }
}

void REditorNotificationView::UpdateItemWidgets()
{
    ItemsParentWidget->ClearSubWidgets();

    for(int64_t i = 0; i < ItemViews.Num(); ++i)
    {
        auto ItemWidget = NewWidget<RWidget>(ItemsParentWidget);
        ItemWidget->Visible=[this,i]()->bool
        {
            return ViewModel->IsItemVisible(i);
        };
        ItemWidget->Anchor.HorizontalFill(ItemsParentWidget);
        ItemWidget->Height = [this,i]()->float
        {
            return ViewModel->GetItemHeight(i);
        };


        ItemViews[i]->FillWidget(ItemWidget);
    }

}
